/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package explorer;

import java.io.*;
 
/**
 * FileTool is a class which provides some methods used to work on Files and Folders.

 * @author laetitia
 */
public final class FileTools {
 
	/**
	 * FileTools Constructor.
	 */
	public FileTools() {
	}
 
	/**
	 * Check if a specified file path is a folder and create a folder if it does
	 * not exist.
	 * 
	 * @param folderPath A folder path.
	 */
	public static void checkFolder(String folderPath) {
		File file = new File(folderPath);
		if (!(file.isDirectory())) {
			file.mkdir();
		}
	}
 
	/**
	 * Delete a directory.
	 * 
	 * @param path A folder path.
	 */
	public static void deleteDirectory(File path) {
		if (path.exists()) {
			File[] files = path.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteDirectory(files[i]);
				} else {
					files[i].delete();
				}
			}
		}
		path.delete();
	}
 
	/**
	 * Move a file from a source to a destination. If the moving by using the
	 * renameTo method does not work, it used the copy method.
	 * 
	 * @param src Source file path.
	 * @param dst Destination file path.
	 */
	public static void move(String src, String dst) {
		File source = new File(src);
		File destination = new File(dst);
		// Try to use renameTo
		boolean result = source.renameTo(destination);
		if (!result) {
			// Copy
			copy(src, dst);
		} 
        
        removeFile(src, "/");
	}
    
    /**
     * 
     * @param inStream
     * @param outStream
     * @param bufferSize
     * @throws IOException 
     */
    public static void actualCopy(final InputStream inStream, final OutputStream outStream, final int bufferSize) throws IOException {
     final byte[] buffer = new byte[bufferSize];
     int nbRead;
     while ((nbRead = inStream.read(buffer)) != -1) {
      outStream.write(buffer, 0, nbRead);
     }
    }
    
    /**
     * Copy all files and directories from a Folder to a destination Folder.
     * 
     * @param from
     * @param to
     * @throws IOException 
     */
    public static void copyDirectory(final File from, final File to) throws IOException {
     if (! to.exists()) {
      to.mkdir();
     }
     
     final File[] inDir = from.listFiles();
     for (int i = 0; i < inDir.length; i++) {
      final File file = inDir[i];
      copySourceFile(file, new File(to, file.getName()));
     }
    }
    
    /**
     * 
     * @param from
     * @param to
     * @throws IOException 
     */
    public static void copySourceFile(final File from, final File to) throws IOException {
     if (from.isFile()) {
      copyFile(from, to);
     } else if (from.isDirectory()){
      copyDirectory(from, to);
     } else {
      throw new FileNotFoundException(from.toString() + " does not exist" );
     }
    }
    
     /**
      * 
      * @param from
      * @param to
      * @throws IOException 
      */   
    public static void copyFile(final File from, final File to) throws IOException {
     final InputStream inStream = new FileInputStream(from);
     final OutputStream outStream = new FileOutputStream(to);
     actualCopy(inStream, outStream, (int) Math.min(from.length(), 4*1024));
     inStream.close();
     outStream.close();
    }
    
    
	/**
	 * Copy a file from a source to a destination.
	 * 
	 * @param src Source file path.
	 * @param dst Destination file path.
	 */
	public static void copy(String src, String dst) {
		File source = new File(src);
		File destination = new File(dst + "/" + source.getName());
		FileInputStream sourceFile = null;
		FileOutputStream destinationFile = null;
        System.out.println("Source = " + source.getAbsolutePath() + " Destination = " + destination.getAbsolutePath());
		try {
			destination.createNewFile();
			sourceFile = new FileInputStream(source);
			destinationFile = new java.io.FileOutputStream(destination);
			// Read by 0.5MB segment.
			byte buffer[] = new byte[512 * 1024];
			int nbRead;
			while ((nbRead = sourceFile.read(buffer)) != -1) {
				destinationFile.write(buffer, 0, nbRead);
			}
			sourceFile.close();
			destinationFile.close();
		} catch (Exception e) {
		}
	}
 
    
	/**
	 * Remove a file in a specified root directory.
	 * 
	 * @param file A file path.
	 * @param rootDirectory A root directory.
	 */
	public static void removeFile(String file, String rootDirectory) {
		// Remove a file on the local machine
		if (file.equalsIgnoreCase("") || file == null) {
		}
		File dir = new File(rootDirectory);
		if (!dir.isDirectory()) {
		} else {
			String filename;
			if (rootDirectory.charAt(rootDirectory.length() - 1) == '/') {
				filename = rootDirectory + file;
			} else {
				filename = rootDirectory + "/" + file;
			}
			File f = new File(filename);
			if (f.exists()) {
				f.delete();
			}
		}
	}
}
